package astaction;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;

import ast.ASTStatData;
import astStatData.ElementMap;

public class ReferenceTracer extends ASTVisitor{
//	private ASTStatData statDataSet;
	private ElementMap map;

	public ReferenceTracer(ElementMap m){
		map = m;
	
//		statDataSet = new ASTStatData();
		
	}
	
	public boolean visit(ClassInstanceCreation node){
		return true;
	}
	
	public boolean visit(MethodInvocation node){
		IMethodBinding b = node.resolveMethodBinding();
		ITypeBinding classb = b.getDeclaringClass();

//		findCalleeNode();
//		if(foundNode!=null) System.out.println("Found NODE TYPE: "+foundNode.getNodeType());
		
//		ITypeBinding callerNode = (ITypeBinding) map.get(b.getKey());	// get caller
		
		
		System.out.println(//"caller: " + callerNode.getName()
							 "\tcallee: " + classb.getName());
		
		return true;
	}
	
	public void preVisit(ASTNode node){
		if(node instanceof CompilationUnit){
			System.out.println("--------------------START RefTrace--------------------\n");
		}
	} 
	

	public void postVisit(ASTNode node){
		if(node instanceof CompilationUnit){
			System.out.println("--------------------END RefTrace--------------------\n");
		}
	}
	
	public void findCalleeNode(){
		HashSet<String> keys = (HashSet<String>) map.getKeys();
		System.out.println( keys.size() );
//		HashSet cus = (HashSet) map.getCUs();
		
		Iterator<String> itr = keys.iterator();
		System.out.println("while start");
//		System.out.println("cu number" + cus.size());
		while(itr.hasNext()){
			
			String k = (String) itr.next();
			System.out.println(k);
		}
		System.out.println("while END");
		
		
	}
}
